package cn.iocoder.yudao.module.biz.controller.admin.trainingRecord;

import cn.iocoder.yudao.module.infra.dal.dataobject.oss.OssDO;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.biz.controller.admin.trainingRecord.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.trainingRecord.BizTrainingRecordDO;
import cn.iocoder.yudao.module.biz.convert.trainingRecord.BizTrainingRecordConvert;
import cn.iocoder.yudao.module.biz.service.trainingRecord.BizTrainingRecordService;

@Api(tags = "管理后台 - 培训记录")
@RestController
@RequestMapping("/biz/training-record")
@Validated
public class BizTrainingRecordController {

    @Resource
    private BizTrainingRecordService trainingRecordService;

    @PostMapping("/create")
    @ApiOperation("创建培训记录")
    @PreAuthorize("@ss.hasPermission('biz:training-record:create')")
    public CommonResult<String> createTrainingRecord(@Valid @RequestBody BizTrainingRecordCreateReqVO createReqVO) {
        return success(trainingRecordService.createTrainingRecord(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新培训记录")
    @PreAuthorize("@ss.hasPermission('biz:training-record:update')")
    public CommonResult<Boolean> updateTrainingRecord(@Valid @RequestBody BizTrainingRecordUpdateReqVO updateReqVO) {
        trainingRecordService.updateTrainingRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除培训记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:training-record:delete')")
    public CommonResult<Boolean> deleteTrainingRecord(@RequestParam("id") String id) {
        trainingRecordService.deleteTrainingRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得培训记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:training-record:query')")
    public CommonResult<BizTrainingRecordPageJoinVO> getTrainingRecord(@RequestParam("id") String id) {
        BizTrainingRecordPageReqVO vo=new BizTrainingRecordPageReqVO();
        vo.setPageNo(0);
        vo.setPageSize(10);
        vo.setId(id);
        List<BizTrainingRecordPageJoinVO> list = trainingRecordService.selectTrainingRecord(vo);
        BizTrainingRecordPageJoinVO joinVO=list.get(0);

        if(!ObjectUtils.isEmpty(joinVO.getTrainingCourseId())){
            OssDO trainingCourseOss=new OssDO();
            trainingCourseOss.setOriginalName(joinVO.getCourseName());
            trainingCourseOss.setUrl(joinVO.getCourseUrl());
            trainingCourseOss.setId(Long.parseLong(joinVO.getTrainingCourseId()));
            List<OssDO> ossDOList = new ArrayList<>();
            ossDOList.add(trainingCourseOss);
            joinVO.setTrainingCourseOss(ossDOList);
        }

        if(!ObjectUtils.isEmpty(joinVO.getSiteImageId())){
            OssDO siteImageOss=new OssDO();
            siteImageOss.setUrl(joinVO.getSiteUrl());
            siteImageOss.setId(Long.parseLong(joinVO.getSiteImageId()));
            List<OssDO> ossDOList = new ArrayList<>();
            ossDOList.add(siteImageOss);
            joinVO.setSiteImageOss(ossDOList);
        }

        if(!ObjectUtils.isEmpty(joinVO.getSigninImageId())){
            OssDO signinOss=new OssDO();
            signinOss.setUrl(joinVO.getSigninUrl());
            signinOss.setId(Long.parseLong(joinVO.getSigninImageId()));
            List<OssDO> ossDOList = new ArrayList<>();
            ossDOList.add(signinOss);
            joinVO.setSigninImageOss(ossDOList);
        }
        return success(joinVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得培训记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:training-record:query')")
    public CommonResult<List<BizTrainingRecordRespVO>> getTrainingRecordList(@RequestParam("ids") Collection<String> ids) {
        List<BizTrainingRecordDO> list = trainingRecordService.getTrainingRecordList(ids);
        return success(BizTrainingRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得培训记录分页")
    @PreAuthorize("@ss.hasPermission('biz:training-record:query')")
    public CommonResult<PageResult<BizTrainingRecordRespVO>> getTrainingRecordPage(@Valid BizTrainingRecordPageReqVO pageVO) {
        PageResult<BizTrainingRecordDO> pageResult = trainingRecordService.getTrainingRecordPage(pageVO);
        return success(BizTrainingRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping(value = "/selectTrainingRecord")
    public PageResult<BizTrainingRecordPageJoinVO> selectTrainingRecord(BizTrainingRecordPageReqVO pageVO) {
        PageResult<BizTrainingRecordPageJoinVO> result=new PageResult<>();
        Integer pageNo=pageVO.getPageNo();
        Integer pageSize=  pageVO.getPageSize();
        //设置为limit开始的条数
        pageVO.setPageNo((pageNo-1)*pageSize);
        List<BizTrainingRecordPageJoinVO> list = trainingRecordService.selectTrainingRecord(pageVO);
        Integer count= trainingRecordService.selectTrainingRecordCount(pageVO);
        result.setList(list);
        result.setTotal((long) count);
        return result;
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出培训记录 Excel")
    @PreAuthorize("@ss.hasPermission('biz:training-record:export')")
    @OperateLog(type = EXPORT)
    public void exportTrainingRecordExcel(@Valid BizTrainingRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizTrainingRecordDO> list = trainingRecordService.getTrainingRecordList(exportReqVO);
        // 导出 Excel
        List<BizTrainingRecordExcelVO> datas = BizTrainingRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "培训记录.xls", "数据", BizTrainingRecordExcelVO.class, datas);
    }

}
